﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets
{
    public class BrushPickerComboBox : ComboBox
    {
        public BrushPickerComboBox()
        {
            foreach (var bi in BrushManager.BrushesList)
            {
                this.Items.Add(new BrushPickerComboBoxItem(bi));
            }
        }

        internal void Select(Brush brush)
        {
            for (int i = 0; i < this.Items.Count; i++)
            {
                var item = this.Items[i] as BrushPickerComboBoxItem;
                if (brush == item.BrushInfo.Brush)
                {
                    this.SelectedIndex = i;
                    return;
                }
            }

            this.SelectedIndex = -1;
        }

        public Brush SelectBrush
        {
            get
            {
                if (this.SelectedIndex < 0) return null;
                return (this.SelectedItem as BrushPickerComboBoxItem).BrushInfo.Brush;
            }
        }

    }

    public class BrushPickerComboBoxItem : ComboBoxItem
    {
        public BrushPickerComboBoxItem(BrushItem bi)
        {
            brushInfo = bi;
            rectangle.Fill = bi.Brush;
            textBlock.Text = bi.CnName;
            brushName = bi.EnName;

            basePanel.Children.Add(rectangle);
            basePanel.Children.Add(textBlock);

            this.Content = basePanel;
        }

        private BrushItem brushInfo;

        public BrushItem BrushInfo
        {
            get { return brushInfo; }
        }

        private string brushName;
        /// <summary>
        /// 画刷的英文名称。
        /// </summary>
        public string BrushName
        {
            get { return brushName; }
        }

        private Rectangle rectangle = new Rectangle()
        {
            Height= 16,
            Width = 16,
            Margin = new System.Windows.Thickness(2, 2, 5, 2),
            VerticalAlignment = System.Windows.VerticalAlignment.Center,
        };

        private TextBlock textBlock = new TextBlock()
        {
            Margin = new System.Windows.Thickness(2, 2, 5, 2),
            VerticalAlignment = System.Windows.VerticalAlignment.Center,
        };

        private StackPanel basePanel = new StackPanel() { Orientation = Orientation.Horizontal, };
    }
}
